package com.luckyframe.project.jmeter.jmeterNode.controller;

import com.alibaba.fastjson.JSONObject;
import com.luckyframe.common.netty.NettyServer;
import com.luckyframe.common.utils.poi.ExcelUtil;
import com.luckyframe.framework.aspectj.lang.annotation.Log;
import com.luckyframe.framework.aspectj.lang.enums.BusinessType;
import com.luckyframe.framework.config.JmeterConfig;
import com.luckyframe.framework.web.controller.BaseController;
import com.luckyframe.framework.web.domain.AjaxResult;
import com.luckyframe.framework.web.page.TableDataInfo;
import com.luckyframe.project.jmeter.jmeterJarMgt.service.IJmeterJarMgtService;
import com.luckyframe.project.jmeter.jmeterNode.domain.JmeterNode;
import com.luckyframe.project.jmeter.jmeterNode.service.IJmeterNodeService;
import com.luckyframe.project.jmeter.jmeterTestCase.service.IJmeterTestCaseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  执行测试计划
 * 
 * @author luckyframe
 * @date 2020-09-11
 */
@Controller
@RequestMapping("/jmeter/execute")
public class JmeterExecuteController extends BaseController
{

	private static final Logger log = LoggerFactory.getLogger(JmeterExecuteController.class);


	private String prefix = "jmeter/execute";
	
	@Autowired
	private IJmeterNodeService jmeterNodeService;

	@Autowired
	private NettyServer nettyServer;

	@Autowired
	private IJmeterTestCaseService jmeterTestCaseService;

	@Autowired
	private IJmeterJarMgtService jmeterJarMgtService;

	@Autowired
	private JmeterConfig jmeterConfig;


	@RequiresPermissions("jmeter:jmeterNode:view")
	@GetMapping()
	public String jmeterNode()
	{
	    return prefix + "/jmeterNode";
	}
	
	/**
	 * 查询性能测试分布式节点列表
	 */
	@RequiresPermissions("jmeter:jmeterNode:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(JmeterNode jmeterNode)
	{
		startPage();
        List<JmeterNode> list = jmeterNodeService.selectJmeterNodeList(jmeterNode);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出性能测试分布式节点列表
	 */
	@RequiresPermissions("jmeter:jmeterNode:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JmeterNode jmeterNode)
    {
    	List<JmeterNode> list = jmeterNodeService.selectJmeterNodeList(jmeterNode);
        ExcelUtil<JmeterNode> util = new ExcelUtil<JmeterNode>(JmeterNode.class);
        return util.exportExcel(list, "jmeterNode");
    }
	
	/**
	 * 新增性能测试分布式节点
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存性能测试分布式节点
	 */
	@RequiresPermissions("jmeter:jmeterNode:add")
	@Log(title = "性能测试分布式节点", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(JmeterNode jmeterNode)
	{		
		return toAjax(jmeterNodeService.insertJmeterNode(jmeterNode));
	}

	/**
	 * 修改性能测试分布式节点
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap)
	{
		JmeterNode jmeterNode = jmeterNodeService.selectJmeterNodeById(id);
		mmap.put("jmeterNode", jmeterNode);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存性能测试分布式节点
	 */
	@RequiresPermissions("jmeter:jmeterNode:edit")
	@Log(title = "性能测试分布式节点", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(JmeterNode jmeterNode)
	{		
		return toAjax(jmeterNodeService.updateJmeterNode(jmeterNode));
	}
	
	/**
	 * 删除性能测试分布式节点
	 */
	@RequiresPermissions("jmeter:jmeterNode:remove")
	@Log(title = "性能测试分布式节点", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(jmeterNodeService.deleteJmeterNodeByIds(ids));
	}


	/**
	 * 根据客户端ID获取状态
	 * @param clientId 客户端ID
	 * @return 返回客户端状态
	 * @author Seagull
	 * @date 2019年8月16日
	 */
	@GetMapping("/getClientStatusByClientId/{clientId}")
	@ResponseBody
	public String getClientStatusByClientId(@PathVariable("clientId") Long clientId)
	{
		JmeterNode client = jmeterNodeService.selectJmeterNodeById(clientId);
		return JSONObject.toJSONString(client);
	}



}
